package com.xdh.zzqh.ui

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.xdh.zzqh.R
import com.xdh.zzqh.utils.globalOrderInfo
import com.xdh.zzqh.utils.setOrder
import com.xdh.zzqh.utils.sharedViewImage
import com.xdh.zzqh.utils.stringById
import java.math.BigDecimal

private val TABLE_HEADER = listOf(stringById(R.string.lei_xing), stringById(R.string.kuan_shi), stringById(R.string.gui_ge), stringById(R.string.kai_xiang), stringById(R.string.zuo), stringById(R.string.you), stringById(R.string.jia_ge), stringById(R.string.jin_e), stringById(R.string.la_shou), stringById(R.string.yan_se), stringById(R.string.bei_zhu))

class OrderActivity : BaseActivity() {

    private val table: RecyclerView by lazy { findViewById(R.id.id_order_table) }
    private val tvName: TextView by lazy { findViewById(R.id.id_order_tv_name) }
    private val tvDate: TextView by lazy { findViewById(R.id.id_order_tv_date) }
    private val layout: ConstraintLayout by lazy { findViewById(R.id.id_order_layout) }
    private val tvLeft: TextView by lazy { findViewById(R.id.id_order_tv_left_num) }
    private val tvRight: TextView by lazy { findViewById(R.id.id_order_tv_right_num) }
    private val tvNum: TextView by lazy { findViewById(R.id.id_order_tv_num) }
    private val tvAllPrice: TextView by lazy { findViewById(R.id.id_order_tv_price) }
    private val btnShared: Button by lazy { findViewById(R.id.id_order_btn_shared) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_order)
        init()
    }

    private fun init() {

        val info = globalOrderInfo!!
        tvName.text = info.name
        tvDate.text = info.date

        var left = 0
        var right = 0
        var all = BigDecimal(0)
        val list = ArrayList<String>()
        list.addAll(TABLE_HEADER)
        for (item in info.order) {
            val sub = ArrayList<String>()
            sub.add(item.type)
            sub.add(item.style)
            sub.add(item.spec)
            sub.add(item.dir)
            if (item.left == 0) {
                sub.add(" ")
            } else {
                sub.add(item.left.toString())
            }
            if (item.right == 0) {
                sub.add(" ")
            } else {
                sub.add(item.right.toString())
            }
            sub.add(item.price.toString())
            sub.add(item.allPrice())
            sub.add(item.hands)
            sub.add(item.color)
            sub.add(item.remark)
            left += item.left
            right += item.right
            all = all.add(BigDecimal(item.allPrice()))

            list.addAll(sub)
        }
        setOrder(table, list)

        tvLeft.text = "${stringById(R.string.zuo_shu_liang)}:$left"
        tvRight.text = "${stringById(R.string.you_shu_liang)}:$right"
        tvNum.text = "${stringById(R.string.he_ji_shu_liang)}:${left + right}"
        tvAllPrice.text = "${stringById(R.string.he_ji_jin_e)}:$all"

        btnShared.setOnClickListener {
            sharedViewImage(layout)
        }
    }
}


